<fieldset style="padding: 5px 5px 5px 5px">
<fieldset>
<form action="" method="get">
    <table border="0" cellpadding="3" cellspacing="3">
        <tr>
            <td align="right" valign="middle">Start:</td>
            <td><%= calendar_date_select_tag "start", params["start"], :popup => :force, :time => true, :valid_date_check => "date <= (new Date()).stripTime()" %></td>
            <td align="right" valign="middle">End:</td>
            <td><%= calendar_date_select_tag "end", params["end"], :popup => :force, :time => true, :valid_date_check => "date <= (new Date()).stripTime()" %></td>
        </tr>
        <tr>
            <td>Cost/kWh $</td>
            <td><input type="text" name="price" value="<%= params['price'] %>"></td>
            <td>&nbsp;</td>
            <td><input type="submit" value="Generate Report" /></td>
        </tr>
    </table>
</form>
</fieldset>
<% if params["start"] and params["end"] -%>
<fieldset>
<legend><b> Pod </b></legend>
<% @pod.each { |row, r| -%>

    <fieldset style="background-color: white">
    <legend><b> <%= row %> </b></legend>
        <h3><span style="padding: 5px 5px 5px 5px; background-color: lightgreen"><%= format("%.4f", @totals[0][row]) if @totals[0][row] %> kWh <% if @totals[2][row] > 0 -%>( $<%= format("%.2f", @totals[2][row]) %> )<% end -%></span></h3>
	<ul>
	<% 
	@pod[row].each { |rack|
		rack.each { |label, circuits|
	-%>
		<li>
			<div><h4><%= label %></h4></div>
			<div>
        		<table border="2" style="border-collapse: collapse;">
            		<tr>
                		<td width="200">&nbsp;</td>
            			<% @times.each { |time| -%>
                		<td width="200" align="center" style="padding: 5px 5px 5px 5px; font-size: 13px; font-weight: bold;"><%= time %></td>
            			<% } -%>
						<td width="200" align="center" style="padding: 5px 5px 5px 5px; font-size: 13px; font-weight: bold; background-color: lightgreen; border-left: 3px solid black">Total</td>
            		</tr>
					<% 
					alternating = -1 
		
					# Keep total per day per rack
					rack_daily_total = {}

					# Rack Total
					rack_total = 0

					circuits.each { |circuit, rrd|

						circuit_total = {}
						alternating  *= -1
					-%>
					<tr style="background-color: <%= 'lightgrey' if alternating == 1 %>">
						<td width="200" align="right" style="padding: 5px 5px 5px 5px; font-size: 13px; font-weight: bold;">
							<%= circuit %>
						</td>
						<% 
						@times.each { |time| 
						
							# Add to circuit total
							circuit_total[circuit] = circuit_total[circuit] ? circuit_total[circuit] + rrd[1][time] : rrd[1][time]

							# Add daily rack total
							rack_daily_total[time] = rack_daily_total[time] ? rack_daily_total[time] + rrd[1][time] : rrd[1][time]

							rack_total += rrd[1][time] 
						-%>
						<td width="200" align="right" style="padding: 5px 5px 5px 5px; font-size: 13px">
							<%= format("%.4f", rrd[1][time]) if rrd[1][time] %>
						</td>
						<% } -%>
						<!-- Circuit Total -->
						<td width="200" align="right" style="padding: 5px 5px 5px 5px; font-size: 13px; font-weight: bold; background-color: lightgreen; border-left: 3px solid black">
							<%=  format("%.4f", circuit_total[circuit]) if circuit_total[circuit] %>
						</td>
					</tr>
					<%
					}
					-%>
            		<tr style="background-color: lightgreen;">
                		<td width="200" align="right" style="padding: 5px 5px 5px 5px; font-size: 13px; font-weight: bold; border-top: 3px solid black;">Daily Total</td>
            			<% @times.each { |time| -%>
                		<td width="200" align="right" style="padding: 5px 5px 5px 5px; font-size: 13px; border-top: 3px solid black; font-weight: bold">
                    		<%= format("%.4f", rack_daily_total[time]) if rack_daily_total[time] %>
                		</td>
            			<% } -%>
						<td width="200" align="right" style="padding: 5px 5px 5px 5px; font-size: 13px; font-weight: bold; background-color: lightgreen; border-top: 3px solid black; border-left: 3px solid black">
							<%= format("%.4f", rack_total) if rack_total %>
						</td>
            		</tr>
				</table>
			</div>
		</li>
	<% 
		}
	} 
	-%>
    </fieldset>
	</ul>
<% } -%>
</fieldset>
<% end -%>
</fieldset>
